package com.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.mapper.SysUserMapper;
import com.demo.pojo.PageBean;
import com.demo.pojo.SysUser;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser getlogin(String usrName, String usrPassword) {
//        Md5Hash hash=new Md5Hash(usrPassword);
        return sysUserMapper.getlogin(usrName,usrPassword);
    }

    @Override
    public PageBean<SysUser> pages(String usrName, Long usrRoleId, Integer pageIndex) {
        System.out.println(usrRoleId+"aa");
        PageBean<SysUser> pageBean=new PageBean<>();

        //数据库第几行开始查询
        int startPage=(pageIndex-1)*pageBean.getPageCount();
        //查询多少行数据 分页类里默认5行
        int selectCount=pageBean.getPageCount();
        //获取mapper层的数据
        List<SysUser> list=sysUserMapper.lists(usrName,usrRoleId,startPage,selectCount);

        QueryWrapper<SysUser> qw=new QueryWrapper<>();
        Map map=new HashMap();
        map.put("usr_name",usrName);
        map.put("usr_role_id",usrRoleId);
        qw.allEq(map,false);
        //获取总数
        int cityCount=sysUserMapper.selectCount(qw);
        pageBean.setCurrent(pageIndex);
        //设置查询的数据
        pageBean.setRecords(list);
        //共多少行
        pageBean.setTotal(cityCount);
        //设置总页数
        pageBean.setTotalPage(cityCount);
        System.out.println("总记录数 ---- --> " + pageBean.getTotal());
        System.out.println("总页数 ------ --> " + pageBean.getPages());
        System.out.println("当前页数 ----- -> " + pageBean.getCurrent());
        System.out.println("每页记录数 -----> " + pageBean.getPageCount());
        System.out.println("当前记录： -----> ");
        return pageBean;
    }

    @Override
    public void saveUser(SysUser sysUser) {

    }

    @Override
    public void deleteUser(Long usrId) {

    }


    @Cacheable(value = "user", keyGenerator = "keyGenerator")
    public SysUser getUser(Long usrId){
        return sysUserMapper.selectById(usrId);
    }


}
